import java.io.*;
import java.net.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class SoftwareHandler extends Thread
{
	private Socket clientSocket;
	private InputStreamReader isr;
	private OutputStream os;
	private Connection connection;

	public SoftwareHandler(Socket clientSocket,SharedObject sharedObject,int clientID)
	{
		this.clientSocket = clientSocket;
		sharedObject.addSoftwareClient(clientSocket, clientID);
	}
	
	// This class isn't used any longer.. The only thing that is we use is to save the socket to shared object.
	// Before a while ago we saved all the software devices in a table named virutalDevice. But since a time back we changed it to the table Devices and in Devices you can find all physical devices also.
	public void run(){
		readDatabase();
		try {
			isr=new InputStreamReader(clientSocket.getInputStream());
			os=clientSocket.getOutputStream();
			
			BufferedReader br = new BufferedReader(isr);
			PrintWriter pw = new PrintWriter(os,true);
			
			pw.println("request");
			String deviceStates = br.readLine();

			updateDeviceStates(deviceStates);
			
			while(!clientSocket.isClosed())
			{
//				if(clientSocket.getInputStream().available()>0){
//				String unit=br.readLine();
//				
//				String parse[]=unit.split(":");
//				
//				try {
//					Statement st=connect.createStatement();
//					if(parse.length>0){
//					st.execute("UPDATE devices SET state='" + parse[1] + "' WHERE deviceName='" + parse[0] + "';");
//					}
//				} catch (SQLException e) {
//					e.printStackTrace();
//				}
//					//pw.println(unit);
//				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}	
	}
	
	//establish a connection the the database
	public void readDatabase()
	{
		try 
		{
			Class.forName("com.mysql.jdbc.Driver");
			connection = DriverManager.getConnection("jdbc:mysql://localhost/projectdb", "root", "");
			Statement stmt = connection.createStatement();
			stmt.execute("USE projectdb");
		} 
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	// Updating device state in database
	public void updateDeviceStates(String deviceStates)
	{
		String[] devices = deviceStates.split(",");
		Statement stmt = null;
		try {
			stmt = connection.createStatement();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		for(int i =0; i < devices.length; i++)
		{
			String[] update = devices[i].split(":");
			ResultSet rs;
			try {
				rs = stmt.executeQuery("SELECT deviceName,state FROM devices WHERE deviceName='" + update[0] + "';");
				if(rs.next())
				{
					stmt.execute("UPDATE devices SET state='off' WHERE deviceName='" + update[0] + "';");	
				}
				else
				{
					stmt.executeUpdate("INSERT INTO devices values (default, '"+ update[0] +"','off');");
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}		
		}
	}
}